package com.help10000.rms.ui.activitys.mine.msg

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.amir.common.beans.MsgCatalog
import com.help10000.rms.R
import com.help10000.rms.mvp.presenter.UserMessageCenterPresenter
import com.help10000.rms.mvp.view.MessageCenterView
import com.help10000.rms.ui.base.BaseActivity
import com.help10000.rms.ui.fragments.user.MessageFragment
import kotlinx.android.synthetic.main.activity_message_center.*

class MessageCenterActivity : BaseActivity<UserMessageCenterPresenter, MessageCenterView>(),
    MessageCenterView {
    var data: List<MsgCatalog>? = null
    override fun getlayoutId(): Int {
        return R.layout.activity_message_center
    }

    override fun initView() {

    }

    override fun initData(savedInstanceState: Bundle?) {

    }

    override fun createPresenter(): UserMessageCenterPresenter {
        return UserMessageCenterPresenter(this)
    }

    override fun initListener() {
        super.initListener()
        mPresenter.getMessageCatalogs()
    }

    private fun initFragments() {

        var adapter = object :
            androidx.fragment.app.FragmentPagerAdapter(supportFragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
            override fun getCount(): Int {
                return fragments.size
            }

            override fun getItem(position: Int): Fragment {
                return fragments.get(position)
            }

            override fun getPageTitle(position: Int): CharSequence? {
                return data!![position].CatgName
            }
        }
        viewpager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {

            }

            override fun onPageSelected(position: Int) {
                //tablayout.selectTab(tablayout.getTabAt(position))
            }

            override fun onPageScrollStateChanged(state: Int) {

            }
        })
        viewpager.adapter = adapter
        tabLayout.setupWithViewPager(viewpager)

    }

    var fragments = mutableListOf<Fragment>()
    override fun onDataSuccess(data: List<MsgCatalog>) {
        this.data = data
        for (index in data.indices) {
            fragments.add(MessageFragment.newInstance(data[index].CatgId))
        }
        initFragments()

    }
}